package dao;

import mapper.Usermapper;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import entity.User;
public  class userdao implements Usermapper{

	@Override
	public Boolean CreatAccount(User user) {
		String resource ="mybatis-config.xml";
		InputStream inputstream = null;
		try {
			inputstream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
		SqlSession session = sqlsessionfactory.openSession();
		Usermapper mapper = session.getMapper(Usermapper.class);
		Boolean iuser = mapper.CreatAccount(user);
		if(iuser == true){
			session.commit();
		}else{
			session.rollback();
		}
		session.close();
		return iuser;
	}

	@Override
	public void Change_Name(User user) {
		String resource ="mybatis-config.xml";
		InputStream inputstream = null;
		try {
			inputstream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
		SqlSession session = sqlsessionfactory.openSession();
		Usermapper mapper = session.getMapper(Usermapper.class);
		mapper.Change_Name(user);
		
		session.commit();
		session.close();
		
		
	}

	@Override
	public void Change_Pwd(User user) {
		String resource ="mybatis-config.xml";
		InputStream inputstream = null;
		try {
			inputstream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
		SqlSession session = sqlsessionfactory.openSession();
		Usermapper mapper = session.getMapper(Usermapper.class);
		mapper.Change_Pwd(user);
		
		session.commit();
		session.close();
		
		
	}

}
